package com.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wms.entity.WStore;
import com.wms.dao.WStoreDao;
import com.wms.entity.WUser;
import com.wms.mytool.Result;
import com.wms.service.WStoreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 仓库信息表(WStore)表服务实现类
 *
 * @author makejava
 * @since 2023-10-20 17:20:40
 */
@Service("wStoreService")
public class WStoreServiceImpl implements WStoreService {
    @Resource
    private WStoreDao wStoreDao;

    /**
     * 查询所有
     * @return 实例对象
     */
    @Override
    public Result selectAll() {
        List<WStore> wStores = this.wStoreDao.selectByMap(null);
        if(wStores!=null){
            return new Result(200,wStores,"查询成功");
        }else {
            return new Result(400,null,"查询失败");
        }
    }

    /**
     * 根据id查询仓库信息
     * @param sid 仓库id
     * @return 实例对象
     */
    @Override
    public WStore selectBySid(Integer sid) {
        QueryWrapper<WStore> qw = Wrappers.query();
        qw.eq("store_id", sid);
        return this.wStoreDao.selectOne(qw);
    }

    /**
     * 新增仓库
     * @param wStore 仓库信息
     * @return 实例对象
     */
    @Override
    public Result addWStore(WStore wStore) {
        wStore.setUseNum(0);
        wStore.setCreateTime(new Date());
        wStore.setUpdateTime(new Date());
        int i = this.wStoreDao.insert(wStore);
        if(i==1){
            return new Result(200,i,"添加成功");
        }else{
            return new Result(400,null,"添加失败");
        }
    }

    /**
     * 修改仓库信息
     * @param wStore 仓库信息
     * @return 实例对象
     */
    @Override
    public Result updateById(WStore wStore) {
        wStore.setUpdateTime(new Date());
        int i = this.wStoreDao.updateById(wStore);
        if(i==1){
            return new Result(200,i,"修改成功");
        }else{
            return new Result(400,null,"修改失败");
        }
    }

    /**
     * 删除仓库信息
     * @param id 仓库id
     * @return 实例对象
     */
    @Override
    public Result deleteWStore(Integer id) {
        try {
            QueryWrapper<WStore> qw = Wrappers.query();
            qw.eq("store_id", id);
            int i = this.wStoreDao.delete(qw);
            if(i==1){
                return new Result(200,i,"删除成功");
            }else{
                return new Result(400,null,"删除失败");
            }
        }catch (Exception e){
            System.out.println(e);
            return new Result(400,null,"存在关联商品，不可删除");
        }

    }

}
